package DataConverters.DataReaders.readersImpl.defaultDataReader.CharBuffer;

/**
 * Created by IntelliJ IDEA.
 * User: Nobody
 * Date: 24.04.11
 * Time: 20:38
 * To change this template use File | Settings | File Templates.
 */
public class CharBuffer {

    private final char[] array;
    private int position;
    private final int limit;

    public CharBuffer(char[] array) {
        this.array = array;
        limit = array.length;
    }

    public int position() {
        return position;
    }

    public int limit() {
        return limit;
    }

    public int remaining() {
        return limit - position;
    }

    void position(int position) {
        this.position = position;
    }

    public char get(int index) {
        return array[index];
    }

    public char[] getRemainingChars(int i) throws EndOfBufferException {
        if (i != position()) {
            return get(position(), i - 1);
        } else {
            return get(position(), i);
        }
    }

    public void incrementPosition(int increment) {
        position(position() + increment);
    }

    char[] get(int start, int end) throws EndOfBufferException {
        if (end > limit()) {
            throw new EndOfBufferException();
        }
        int newArrayLength = end - start;
        position += newArrayLength;
        char[] ret = new char[newArrayLength];
        System.arraycopy(array, start, ret, start - start, newArrayLength);
        return ret;
    }

    @Override
    public String toString() {
        String s = new String(array);
        return s.substring(position(), limit());
    }
}
